<?php

namespace App\Http\Controllers;

use App\Models\Banner;
use App\Models\Doctor;
use App\Models\FormDatum;
use App\Models\HospitalInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class HospitalInfoController extends Controller
{

    public function info()
    {
        $model = HospitalInfo::find(1);

        if (!$model)
            return response()
                ->json([
                    'status' => 1,
                    'err' => '未初始化医院数据,请尽快初始化医院数据'
                ]);

        return response()
            ->json([
                'status' => 0,
                'err' => '',
                'storage_link' => asset('storage'),

                'data' => $model,
            ]);

    }

    public function banners()
    {
        $banners = Banner::query()
            ->where('enable', 1)
            ->orderBy('order_n')
            ->get();


        if (!$banners)
            return response()
                ->json([
                    'status' => 1,
                    'err' => '未初始化Banner数据,请尽快初始化Banner数据'
                ]);

        return response()
            ->json([
                'status' => 0,
                'err' => '',
                'storage_link' => asset('storage'),
                'data' => $banners,
            ]);

    }

    public function doctors()
    {
        $doctors = Doctor::query()
            ->where('enable', 1)
            ->orderBy('order_n')
            ->get();

        if (!$doctors)
            return response()
                ->json([
                    'status' => 1,
                    'err' => '未初始化Doctor数据,请尽快初始化Doctor数据'
                ]);

        return response()
            ->json([
                'status' => 0,
                'err' => '',
                'storage_link' => asset('storage'),
                'data' => $doctors,
            ]);

    }

    public function form(Request $request)
    {
        $data = $request->all([
            'name',
            'phone',
            'source',
            'url',
            'data'
        ]);

        $phoneExists = FormDatum::query()
            ->where('source', $data['source'])
            ->where('phone', $data['phone'])
            ->exists();

        if ($phoneExists)
            return response()
                ->json([
                    'status' => 1,
                    'err' => '请勿重复提交电话号码'
                ]);

        FormDatum::create($data);

        return response()
            ->json([
                'status' => 0,
                'err' => '预约成功'
            ]);
    }

}
